package com.welson.db.cryptor.strategy;

import com.welson.db.cryptor.config.DbCryptorProperties;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 加解密策略工厂
 * @author welson
 */
@Component
public class CryptStrategyFactory {
    private final Map<String, CryptStrategy> strategyMap = new ConcurrentHashMap<>();

    public CryptStrategyFactory(DbCryptorProperties properties) {
        // 注册默认策略
        registerStrategy("SM4", new SM4CryptStrategy(properties));
        registerStrategy("AES", new AESCryptStrategy(properties));
    }

    /**
     * 获取加解密策略
     *
     * @param algorithm 算法名称
     * @return 加解密策略
     */
    public CryptStrategy getStrategy(String algorithm) {
        CryptStrategy strategy = strategyMap.get(algorithm.toUpperCase());
        if (strategy == null) {
            throw new IllegalArgumentException("Unsupported algorithm: " + algorithm + ". Only SM4 and AES are supported.");
        }
        return strategy;
    }

    private void registerStrategy(String algorithm, CryptStrategy strategy) {
        strategyMap.put(algorithm.toUpperCase(), strategy);
    }
} 